//
//  SDAnswerDetectionHeaderView.m
//  ovuola
//
//  Created by  liangzhun on 2018/9/29.
//  Copyright © 2018年 CodeSpace. All rights reserved.
//

#import "SDAnswerDetectionHeaderView.h"
#import "SDAnswersDetectionModel.h"
#import "SDAnswerTestData.h"
@implementation SDAnswerDetectionHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
   
}
 */
- (void)setViews
{
    self.indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
   
    [self.collectionView registerNib:[UINib nibWithNibName:@"SDAnswerDetectionHeaderCell" bundle:nil] forCellWithReuseIdentifier:@"SDAnswerDetectionHeaderCell"];
     [self.collectionView setContentInset:UIEdgeInsetsMake(0, 15, 0, 15)];
    
}

/**
 获取线的宽度并设置
 */

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [SDAnswersDetectionModel shareInstance].dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDAnswerDetectionHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SDAnswerDetectionHeaderCell" forIndexPath:indexPath];
    
    SDAnswerTestData *data = [SDAnswersDetectionModel shareInstance].dataArray[indexPath.row];
    [cell.timeLab setText:data.title];
    cell.Hline.hidden = self.indexpath !=indexPath;
    if (@available(iOS 8.2, *)) {
        [cell.timeLab setFont:self.indexpath ==indexPath? MediumFont(21):Font(17)];
    } else {
        [cell.timeLab setFont:self.indexpath ==indexPath? Font(21):Font(17)];
    }
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath == self.indexpath)return;
    self.indexpath = indexPath;
    if (self.block)self.block(self.indexpath);
    [self.collectionView reloadData];
    [self.collectionView scrollToItemAtIndexPath:self.indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *font;
    if (@available(iOS 8.2, *)) {
       font = self.indexpath ==indexPath? MediumFont(21):Font(17);
    } else {
        font = self.indexpath ==indexPath? Font(21):Font(17);
    }
    SDAnswerTestData *data = [SDAnswersDetectionModel shareInstance].dataArray[indexPath.row];
    CGFloat width = [SDMethodTool getTextWidthWithText:data.title Height:21 Font:font] + 5;
    return CGSizeMake(width, 66);
    
}

#pragma mark - UIScrollViewdelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
    if (scrollToScrollStop) {
        [self scrollViewDidEndScroll:scrollView];
    }
    
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        // 停止类型3
        BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (dragToDragStop) {
            [self scrollViewDidEndScroll:scrollView];
        }
    }
}
//#pragma mark - scrollView 滚动停止
- (void)scrollViewDidEndScroll:(UIScrollView *)scrollView {

//    int t = (int)(scrollView.contentOffset.x + 240*kWidth375Scale/2.0)/(240*kWidth375Scale);
//    self.indexpath = [NSIndexPath indexPathForRow:t inSection:0];
//    YMStatiticalHeaderCell * cell = [self.collectionView cellForItemAtIndexPath:self.indexpath];
//    self.HlineWidth.constant = [kAppDelegate getTextWidthWithText:cell.timeLab.text Height:23 Font:Font(21)] + 5;
//    if (self.block)self.block(self.indexpath);
//    [self.collectionView scrollToItemAtIndexPath:self.indexpath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
//    [self.collectionView reloadData];

}


@end
